const app = getApp();
Page({

    // 页面的初始数据
    data: {
        detail_data:{},
        sum:0,
        pinlun_content:'',
        pinlun_pic:'',
        upload_imgpath:'',
        status:0,
        pinjia_id:0
    },

    // 页面的生命周期函数 – 监听页面加载
    onLoad(res) {
        const that = this;
        swan.request({
            url: app.globalData.urlapi + 'orderDetail',
            data:{
                mid:app.globalData.userInfo.mid,
                id:res.id
            },
            success(e){
                console.log(e)
                var sum = 0
                for(var i of e.data.goods){
                    sum = parseInt(sum) + parseInt(i.shop_num)
                }
                that.setData({
                    detail_data:e.data,
                    sum
                })
            }
        });
    },

    go_quxiao(e){
        swan.request({
            url: app.globalData.urlapi + 'cancelOrder',
            data:{
                order_id:e.currentTarget.dataset.order_id
            },
            success(e){
                console.log(e)
                if(e.data.code == 1){
                    swan.showToast({
                        title: e.data.msg,
                        duration:'800'
                    });
                    swan.navigateTo({
                        url: '../order_list/order_list'
                    });
                }else{
                    swan.showToast({
                        title: e.data.msg,
                        icon:'none',
                        duration:'800'
                    }); 
                }
            }
        });
    },

    go_del(e){
        swan.request({
            url: app.globalData.urlapi + 'deleteOrder',
            data:{
                order_id:e.currentTarget.dataset.order_id
            },
            success(e){
                if(e.data.code == 1){
                    swan.showToast({
                        title: e.data.msg,
                        duration:'800'
                    });
                    swan.navigateTo({
                        url: '../order_list/order_list'
                    });
                }else{
                    swan.showToast({
                        title: e.data.msg,
                        icon:'none',
                        duration:'800'
                    }); 
                }
            }
        });
    },

    go_pay(e){
        swan.navigateTo({
            url: '../order_success/order_success?order_id='+e.currentTarget.dataset.order_id
        });
    },

    go_pinjia(e){
        this.setData({
            pinjia_id:e.currentTarget.dataset.id,
            status:1
        })
    },

    t_close(){
        this.setData({
            status:0
        })
    },

    contentHandle(e){
        this.setData({
            pinlun_content:e.detail.value
        })
    },

    file_img(){
        const that = this;
        swan.chooseImage({
            success: res => {
                console.log(res)
                swan.uploadFile({
                    url: app.globalData.urlapi + 'uploadCommentPic',
                    filePath: res.tempFilePaths[0],
                    name: 'myfile',
                    header: {
                        'content-type': 'multipart/form-data'
                    },
                    success(e){
                        var path = JSON.parse(e.data).path[0]
                        that.setData({
                            upload_imgpath:path
                        })
                    }
                });
                that.setData({
                    pinlun_pic:res.tempFilePaths[0]
                })
            }
        });
    },

    sub_pinjia(){
        const that = this;
        swan.request({
            url: app.globalData.urlapi + 'commentOrder',
            method:"POST",
            header: {'content-type': 'application/x-www-form-urlencoded'},
            data:{
                mid:app.globalData.userInfo.mid,
                order_id:that.data.detail_data.id,
                goods_id:that.data.pinjia_id,
                pic:that.data.upload_imgpath,
                content:that.data.pinlun_content,
                score:5
            },
            success(e){
                // console.log(e)
                swan.showToast({
                    title: e.data.msg,
                    duration:'800'
                });
                if(e.data.code == 1){
                    var obj = that.data.detail_data
                    for(var o of obj.goods){
                        if(o.id == that.data.pinjia_id){
                            o.commented = 1
                        }
                    }
                    that.setData({
                        detail_data:obj
                    })
                    that.t_close()
                }
            }
        });
    },

    // 页面的生命周期函数 – 监听页面初次渲染完成
    onReady(res) {

    },

    // 页面的生命周期函数 – 监听页面显示
    onShow(res) {

    },

    // 页面的生命周期函数 – 监听页面隐藏
    onHide(res) {

    },

    // 页面的生命周期函数 – 监听页面卸载
    onUnload(res) {

    },

    // 页面的生命周期函数 – 监听页面重启，单击重启按钮时触发
    onForceReLaunch(res) {

    },

    // 页面的事件处理函数 – 监听用户下拉动作
    onPullDownRefresh(res) {

    },

    // 页面的事件处理函数 – 上拉触底事件的处理函数
    onReachBottom(res) {

    },

    // 页面的事件处理函数 – 用户点击右上角转发
    onShareAppMessage(res) {

    },

    // 页面的事件处理函数 – 页面滚动触发事件的处理函数
    onPageScroll(res) {

    },

    // 页面的事件处理函数 – 当前是 tab 页时，点击 tab 时触发
    onTabItemTap(res) {

    }
});